<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<link href="${ctx }/js/plugs/jquery-ui-1.10.3.custom/css/mycss/jquery-ui-1.10.3.custom.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="${ctx }/js/plugs/bootstrap-3.3.0-dist/dist/css/bootstrap.css">
<link href="${ctx }/js/plugs/font-awesome-4.2.0/css/font-awesome.css" rel="stylesheet">
<link href="${ctx }/css/dw-user.css" rel="stylesheet" type="text/css" />
<link href="${ctx }/js/plugs/jquery-ui-1.10.3.custom/css/mycss/jquery-ui-1.10.3.custom.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="${ctx }/js/plugs/jquery-ui-1.10.3.custom/js/jquery-1.10.1.js"></script>
<script type="text/javascript" src="${ctx }/js/plugs/jquery-ui-1.10.3.custom/js/jquery-ui-1.10.3.custom.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="${ctx }/js/plugs/jquery-ui-1.10.3.custom/css/mycss/jquery-ui-1.10.3.custom.css" rel="stylesheet" type="text/css" />
<!-- 新 Bootstrap 核心 CSS 文件 -->
<!-- <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css"> -->
<link rel="stylesheet" href="${ctx }/js/plugs/bootstrap-3.3.0-dist/dist/css/bootstrap.css">
<!-- 可选的Bootstrap主题文件（一般不用引入） -->
<!-- <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap-theme.min.css"> -->
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<!-- <script src="http://cdn.bootcss.com/bootstrap/3.3.0/js/bootstrap.min.js"></script> -->
<script src="${ctx }/js/plugs/bootstrap-3.3.0-dist/dist/js/bootstrap.js"></script>
<link href="${ctx }/css/dw-user.css" rel="stylesheet" type="text/css" />
<!-- <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet"> -->


<head>
    <title>影响力问卷</title>
</head>
<div id="header">
    <div id="headerCenter" class="bodyCenter" style="position: relative; width: 1359px;">
        <div class="header_Item header_logo">
            <a href="/"><img alt="国际认证评估问卷系统" src="/images/logo/LOGO.png" align="middle" height="46"><span class="titleTempSpan" style="color:#ffa71d;"></span></a>
        </div>
        <div class="header_Item header_menu">
            <ul>
                <li><a href="/design/my-survey/list.do" id="mysurvey">问卷</a></li>
                <li><a href="/sy/user/survey-admin/list.do" id="surveymanager">问卷管理</a></li>
                <li><a href="/sy/user/user-admin/list.do" id="usermanager">用户管理</a></li>
                <li><a href="/sy/group/group-admin/list.do" id="groupmanager">分组管理</a></li>
                <li><a href="/sy/lottery/list.do" id="lotterymanager">问卷奖项</a></li>
                <li><a href="/Insrole/edit.do" id="">机构管理</a></li>
                <li><a href="/sy/system/sys-property/input.do" id="systemset">系统设置</a></li>
                <li><a href="http://www.accreditation.com/">帮助</a></li>
            </ul>
        </div>
        <div class="header_Item header_user" style="float: right;margin-top: 8px;position:absolute;zoom: 1;z-index: 365;display: inline;right: 0px;">
            <a href="#" class="clickHideUserMenu">
						<span class="head_use_name">
						admin
							<i class="fa fa-caret-down" aria-hidden="true"></i>
						</span>

            </a>
            <div class="a-w-sel a-w-sel-head" style="">
                <div class="w-sel" style="margin-top: 16px;">
                    <div class="selc">
                        <div class="selcc tbtag">
                            <div class="seli"><a class="nx-1" href="/ic/user//myaccount.do">修改密码</a></div>
                            <div class="seli"><a class="nx-7" href="http://www.accreditation.com/">帮助及反馈</a></div>
                            <div class="seli"><a class="nx-8" href="/user/logout.do">退出</a></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </div>
    <div style="clear: both;"></div>
</div>
<div id="dwBodyContent" class="bodyCenter" style="width: 1189px;">
    <div id="dwBodyUser">
        <div class="surveyCollectMiddle">
            <div class="surveyCollectMiddleContent">
                <div style="padding: 25px 45px;overflow: auto;padding-top: 20px;">
                    <table class="contacts-table" id="table" style="width:80%;margin-left:10%;margin-top: 30px;text-align: center" border="1" cellspacing="0">
                        <input onclick="addrecord()" type="button" value="添加一条记录" class="sbtn25 sbtn25_1" style="font-size: 16px;margin-left: 80%">
                        <tr id="ttt">

                        </tr>
                    </table>
                    <input onclick="update()" type="button" value="保存" class="sbtn25 sbtn25_1" style="font-size: 16px;margin-left: 10%;margin-top: 5%;display: inline">
                    <input onclick="deleteoneelement()" type="button" value="删除上一条" class="sbtn25 sbtn25_1" style="font-size: 16px;margin-left: 60%;display: inline">
                </div>
            </div>
        </div>
    </div>
</div>
<script>
<%--    第一个ajax用来拿table标题--%>
    //记录上一条单选按钮的name值
    window.numdata="";
    $.ajax({
        type:"get",
        url:"/showTable/findtabletitles.do",
        dataType:"json",
        data:{
            id:${reportdescid}
        },
        success:function (data) {

            var html="";
            var neirong="";
            console.log(data.length);
            window.datalist=[];
            for (let i = 0; i <data.length ; i++) {

                var shuxing=data[i]["cnName"];
                neirong= "<td>"+shuxing+"</td>";
                html=html+neirong;
                //构造类型，字典的list
                var arr={
                    type:data[i]["type"],
                    dict:data[i]["dict"]
                }
                datalist.push(arr);
            }
            document.getElementById("ttt").innerHTML=document.getElementById("ttt").innerHTML+html;
        }
    })

//    第二个ajax用来拿table内容
$.ajax({
    async: false,
    type:"get",
    url:"/showTable/findtablevalues.do?reportdescid=${reportdescid}&civilyear=${civilyear}&orgid=${orgid}",
    dataType:"json",
    success:function (res) {
        data=res.data
        window.data=data;
        var html="";
        var neirong="";
        //定义行内的元素有几个
        var hangsum=data.length;

        //遍历结果中的dict，存到array中
        window.dict=[];
        window.dictArray=[];
        for(let p=0;p<data.length;p++){
            for (let q=0;q<data[p].length;q++){
                if(data[p][q].type=="select"){
                    if(window.dict.indexOf(data[p][q].dict)<0){
                        window.dict.push(data[p][q].dict);
                        window.dictArray.push({type:"select",values:[],dict:data[p][q].dict});
                    }

                }
                else if(data[p][q].type=="Mchoice"){
                    if(window.dict.indexOf(data[p][q].dict)<0){
                        window.dict.push(data[p][q].dict);
                        window.dictArray.push({type:"Mchoice",values:[],dict:data[p][q].dict});
                    }
                }
                else if(data[p][q].type=="Schoice"){
                    if(window.dict.indexOf(data[p][q].dict)<0){
                        window.dict.push(data[p][q].dict);
                        window.dictArray.push({type:"Schoice",values:[],dict:data[p][q].dict});
                    }
                }
            }
        }
        //遍历dictarray中的dict拿到字典字段
        for (let l = 0; l < dict.length; l++) {
            $.ajax({
                async:false,
                type:"get",
                url:"/showTable/findtabledict.do",
                dataType:"json",
                data:{
                    dictid:dict[l]
                },
                success:function (data) {
                    for(let m=0;m<dictArray.length;m++){
                        if(dict[l]==dictArray[m].dict){
                            window.dictArray[m].values=data;
                            break;
                        }
                    }
                    if(l==dict.length-1){
                        for (let i = 0; i <window.data.length ; i++) {
                            var tdcontext1="<tr>";
                            var tdcontext2="</tr>";
                            var tdneirong="";

                            for(let j=0;j<window.data[i].length;j++){
                                //临时变量temp为td的内容
                                var temp="";
                                var temp1="";
                                // debugger
                                var title=window.data[i][j].cnname;
                                console.log(title)

                                if(window.data[i][j].type=="input"){
                                    temp="<td code='input' contenteditable='true' dict="+window.data[i][j].dict+" order="+window.data[i][j].order+">"+window.data[i][j].value+"</td>"
                                    tdneirong=tdneirong+temp;
                                    console.log(tdneirong)

                                }
                                else if(window.data[i][j].type=="select"){

                                    var tdhead="<td code='select' dict="+window.data[i][j].dict+" order="+window.data[i][j].order+"><select onChange='xialakuang(this)' id="+window.data[i][j].order+">";
                                    var tdfoot="</select></td>";
                                    for (let a = 0; a <dictArray.length ; a++) {
                                        if(dictArray[a].type=="select"){

                                            for(let k=0;k<dictArray[a].values.length;k++){
                                                if(window.data[i][j].order==dictArray[a].values[k].Sort_order){
                                                    temp="<option onchange='xialakuang(this);' selected='selected' value='"+dictArray[a].values[k].Sort_order+"'>"+dictArray[a].values[k].Item_text+"</option>";
                                                }
                                                else{
                                                    temp="<option onchange='xialakuang(this);' value='"+dictArray[a].values[k].Sort_order+"'>"+dictArray[a].values[k].Item_text+"</option>";
                                                }
                                                temp1=temp1+temp
                                            }
                                        }
                                    }

                                    tdneirong=tdneirong+tdhead+temp1+tdfoot;
                                    console.log(tdneirong)

                                }
                                else if(window.data[i][j].type=="Mchoice"){
                                    var tdhead="<td code='Mchoice' dict="+window.data[i][j].dict+" order="+window.data[i][j].order+">";
                                    var tdfoot="</td>";
                                    //创建一个list，存不重复的数据
                                    var arrayobj=new Array();
                                    var arraytemp=new Array();
                                    window.kk=arraytemp;
                                    window.kt=arrayobj;
                                        for (let a = 0; a <dictArray.length ; a++) {
                                        if(dictArray[a].type=="Mchoice"){

                                            for(let k=0;k<dictArray[a].values.length;k++){
                                                var Mchoiceorder="";
                                                //添加默认勾选
                                                //多选中只有一个的情况
                                                if(window.data[i][j].order.length==1){
                                                    if(dictArray[a].values[k].Sort_order==window.data[i][j].order && dictArray[a].values[k].Dict_id==window.data[i][j].dict){

                                                        temp="<input type='checkbox' content='"+dictArray[a].values[k].Item_text+"' onclick=duoxuan(this) order='"+window.data[i][j].order+"' dict='"+window.data[i][j].dict+"' name='"+k+"' value='"+window.data[i][j].order+"' checked='checked'>"+dictArray[a].values[k].Item_text;
                                                    }
                                                    else{
                                                        temp="<input type='checkbox' content='"+dictArray[a].values[k].Item_text+"' onclick=duoxuan(this) order='"+window.data[i][j].order+"' dict='"+window.data[i][j].dict+"' name='"+k+"' value='"+window.data[i][j].order+"'>"+dictArray[a].values[k].Item_text;
                                                    }
                                                    temp1=temp1+temp
                                                }
                                                //多选中多个的情况
                                                else{
                                                    window.Mchoiceorder=window.data[i][j].order.replace(/,/g, "");
                                                    //如果第一个元素是未被勾选，同样需被渲染
                                                    // console.log(m);
                                                    //如果最先渲染的值是被勾选的，跳过下一行
                                                    var jt="0";
                                                    //如果第一个元素是未被勾选，同样需被渲染
                                                    var qq="0"

                                                    for(let m=0;m<window.Mchoiceorder.length;m++){



                                                            if(dictArray[a].values[k].Sort_order==window.Mchoiceorder[m] && dictArray[a].values[k].Dict_id==window.data[i][j].dict){
                                                                if(arrayobj.indexOf(k)==-1){
                                                                    arrayobj.push(k);
                                                                    console.log("匹配成功m"+m);
                                                                    temp="<input type='checkbox' content='"+dictArray[a].values[k].Item_text+"' onclick=duoxuan(this) order='"+window.data[i][j].order+"' dict='"+window.data[i][j].dict+"' name='"+k+"' value='"+window.Mchoiceorder[m]+"' checked='checked'>"+dictArray[a].values[k].Item_text;
                                                                    //如果当前列显示出来记录当前列的k值，下一列k值如果和当前列k值相同不展示
                                                                    var tt=k;
                                                                    jt="1";
                                                                    arraytemp.push(temp);
                                                                }
                                                                else{
                                                                    arrayobj.pop();
                                                                    arraytemp.pop();
                                                                    console.log("已经存过了");
                                                                    arrayobj.push(k);
                                                                    console.log("匹配成功m"+m);
                                                                    temp="<input type='checkbox' content='"+dictArray[a].values[k].Item_text+"' onclick=duoxuan(this) order='"+window.data[i][j].order+"' dict='"+window.data[i][j].dict+"' name='"+k+"' value='"+window.Mchoiceorder[m]+"' checked='checked'>"+dictArray[a].values[k].Item_text;
                                                                    //如果当前列显示出来记录当前列的k值，下一列k值如果和当前列k值相同不展示
                                                                    var tt=k;
                                                                    jt="1";
                                                                    arraytemp.push(temp);
                                                                }

                                                            }
                                                            else if(dictArray[a].values[k].Dict_id==window.data[i][j].dict && tt!=k &&(jt!="0"||qq=="0")){
                                                                if(arrayobj.indexOf(k)==-1) {
                                                                    arrayobj.push(k)
                                                                    console.log("匹配失败m" + m);
                                                                    temp = "<input content='"+dictArray[a].values[k].Item_text+"' onclick=duoxuan(this) order='"+window.data[i][j].order+"' type='checkbox' dict='" + window.data[i][j].dict + "' name='" + k + "' value='" + window.Mchoiceorder[m] + "'>" + dictArray[a].values[k].Item_text;
                                                                    qq = "1";
                                                                    arraytemp.push(temp);
                                                                }
                                                                else{
                                                                    console.log("已经存过了")
                                                                }
                                                            }
                                                            else {
                                                                temp="";
                                                                console.log("未匹配m"+m)
                                                            }
                                                            // temp1=temp1+temp
                                                        }

                                                    }
                                                }

                                            }
                                        }

                                    for(let i=0;i<arraytemp.length;i++){
                                        temp1=temp1+arraytemp[i]
                                    }
                                    tdneirong=tdneirong+tdhead+temp1+tdfoot;
                                    console.log(tdneirong)
                                }
                                else if(window.data[i][j].type=="Schoice"){
                                    var tdhead="<td code='Schoice' dict="+window.data[i][j].dict+" order="+window.data[i][j].order+">";
                                    var tdfoot="</td>";
                                    for (let a = 0; a <dictArray.length ; a++) {
                                        if(dictArray[a].type=="Schoice"){
                                            /*生成5位随机数*/
                                            var num = parseInt(Math.random()*100000)
                                            window.numdata=num;

                                            for(let k=0;k<dictArray[a].values.length;k++){


                                                //添加默认勾选
                                                console.log(dictArray[a].values[k].Sort_order)

                                                if(dictArray[a].values[k].Sort_order==window.data[i][j].order && dictArray[a].values[k].Dict_id==window.data[i][j].dict){
                                                    // alert(j)
                                                    temp="<input type='radio' name='"+num+"' onclick='danxuan(this)' centent='"+dictArray[a].values[k].Item_text+"' tt='"+window.data[i][j].dict+"value='"+window.data[i][j].order+" checked='checked' >"+dictArray[a].values[k].Item_text;
                                                }
                                                else{
                                                    temp="<input type='radio' name='"+num+"' onclick='danxuan(this)' centent='"+dictArray[a].values[k].Item_text+"' tt='"+window.data[i][j].dict+"value='"+window.data[i][j].order+"' '>"+dictArray[a].values[k].Item_text;
                                                }
                                                console.log(temp)
                                                temp1=temp1+temp
                                                console.log("输出temp1"+temp1)
                                            }
                                        }
                                    }

                                    tdneirong=tdneirong+tdhead+temp1+tdfoot;
                                    console.log(tdneirong)
                                }
                            }
                            neirong=tdcontext1+tdneirong+tdcontext2;
                            console.log(neirong);
                            html=html+neirong;

                        }
                        document.getElementById("table").innerHTML=document.getElementById("table").innerHTML+html;
                    }
                }
            })
        }

    }
})
</script>
<script>
    //添加一条记录
    //添加验证规则只能一条一条添加
    window.checknum=0;
    function addrecord(){
        if(window.checknum==0){
            var num = parseInt(Math.random()*100000)+1
            // var reg = /\d{4}$/g;
            var str=document.getElementById("table").lastChild.lastChild.innerHTML;
            if(window.numdata!=""){
                str=str.replaceAll(window.numdata,num);
            }
            window.numdata=num;
            console.log(str)
            document.getElementById("table").lastChild.innerHTML=document.getElementById("table").lastChild.innerHTML+str;
            window.checknum++;

        }
        else{
            alert("update之后再添加")
        }

    }
    //保存当前数据
    //把当前数据转成json同时调用updata
    function update(){
        window.returndata={
            data:[]
        }
        window.titledom =document.getElementById("table").getElementsByTagName("tbody")[0];
        window.tabledom =document.getElementById("table").getElementsByTagName("tbody")[1];
        for (let i = 0; i < window.tabledom.rows.length; i++) {

            let array1=[]

            for (let j=0;j<window.tabledom.rows[i].cells.length;j++){
                let obj={
                    cnname:"",
                    type:"",
                    dict:"",
                    order:"",
                    value:""
                }
                if(window.tabledom.rows[i].cells[j].getAttribute("code")=="input"){
                    obj.cnname=window.titledom.rows[0].cells[j].innerText;
                    obj.type="input";
                    obj.value=window.tabledom.rows[i].cells[j].innerText;
                    array1.push(obj);
                }
                else if(window.tabledom.rows[i].cells[j].getAttribute("code")=="select"){
                    obj.cnname=window.titledom.rows[0].cells[j].innerText;
                    obj.type="select";
                    obj.dict=window.tabledom.rows[i].cells[j].getAttribute("dict");
                    obj.order=window.tabledom.rows[i].cells[j].getAttribute("order");
                    array1.push(obj);
                }
                else if(window.tabledom.rows[i].cells[j].getAttribute("code")=="Schoice"){
                    obj.cnname=window.titledom.rows[0].cells[j].innerText;
                    obj.type="Schoice";
                    obj.dict=window.tabledom.rows[i].cells[j].getAttribute("dict");
                    obj.order=window.tabledom.rows[i].cells[j].getAttribute("order");
                    array1.push(obj);
                }
                else if(window.tabledom.rows[i].cells[j].getAttribute("code")=="Mchoice"){
                    obj.cnname=window.titledom.rows[0].cells[j].innerText;
                    obj.type="Mchoice";
                    obj.dict=window.tabledom.rows[i].cells[j].getAttribute("dict");
                    obj.order=window.tabledom.rows[i].cells[j].getAttribute("order");
                    array1.push(obj);
                }
            }
            window.returndata.data.push(array1);
        }
        console.log(window.returndata)
        $.ajax({
            async:false,
            type:"post",
            url:"/showTable/updatetablevalue.do",
            dataType:"json",
            data:{
                value:JSON.stringify(window.returndata),
                reportdescid:${reportdescid},
                civilyear:"2020-2021",
                orgid:${orgid}
            },
            success:function(){

            }
        })
        window.checknum=0;
        window.location.reload();
    }
    //删除一条记录
    function deleteoneelement() {
        document.getElementById("table").lastChild.lastChild.remove();
    }
    //单选设置勾选状态
    function danxuan(obj){
        // console.log(obj.checked)
        console.log("单选按钮");
        if(obj.getAttribute("checked")=="unchecked"){
            for(let i=0;i<obj.parentNode.childNodes.length;i++){
                  obj.parentNode.childNodes[i].setAttribute("checked","unchecked");
            }
            obj.setAttribute("checked","checked");
            obj.checked=true;
        }
        for(let k=0;k<dictArray.length;k++){
            if(dictArray[k].dict==obj.parentNode.getAttribute("dict")){
                for(let h=0;h<dictArray[k].values.length;h++){
                    if(dictArray[k].values[h].Item_text==obj.getAttribute("centent")){
                        obj.parentNode.setAttribute("order",dictArray[k].values[h].Sort_order);
                    }
                }
            }
        }
    }
    //下拉框设置勾选
    function xialakuang(obj) {
        // console.log("123456")
        console.log(obj);
        var objvalues=obj.options[obj.options.selectedIndex].value;
        // console.log(objvalues);
        // var selectorder=obj.getAttribute("value");
        obj.id=objvalues;
        obj.parentNode.setAttribute("order",objvalues);




    }
    //多选设置勾选状态
    function duoxuan(obj) {
        if(obj.getAttribute("checked")=="checked"){
            obj.checked=false;
            obj.setAttribute("checked","unchecked");
        }
        else{
            obj.setAttribute("checked","checked");
            // obj.checked=false;
        }
        obj.parentNode.setAttribute("order","")
        var temp="";
        for(let m=0;m<dictArray.length;m++){
            if(dictArray[m].dict==obj.parentNode.getAttribute("dict")){
                // if(m%2==0){
                //     if(obj.getAttribute("checked")=="checked"){
                //
                //     }
                // }else {
                //     console.log("no message")
                // }
                for(let a=0;a<obj.parentNode.childNodes.length;a++){
                    if(a%2==0){
                        console.log(a);
                        console.log(obj.parentNode.childNodes[a]);
                        if(obj.parentNode.childNodes[a].getAttribute("checked")=="checked"){
                            console.log("捕获到了")
                            for(let o=0;o<dictArray[m].values.length;o++){
                                if(obj.parentNode.childNodes[a].getAttribute("content")==dictArray[m].values[o].Item_text){
                                    if(temp==""){
                                        temp=dictArray[m].values[o].Sort_order;
                                        console.log(temp);
                                        console.log(temp)
                                    }
                                    else{
                                        temp=temp+","+dictArray[m].values[o].Sort_order;
                                        console.log(temp)
                                    }
                                }
                            }

                            // if(temp==""){
                            //
                            // }
                            // else{
                            //     temp=temp+","+
                            // }
                        }
                    }

                    else{
                        console.log("no message")
                    }
                }
            }
            obj.parentNode.setAttribute("order",temp);
        }


    }
</script>

